package com.oohla.ex.seekbar;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity {
    private SeekBar seekBar1;
    private SeekBar musicBar;
    private Button btnPlay;
    private Button btnStop;
    private TextView timeTextView;

    private MediaPlayer player;
    private Timer musicTimer;
    private Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
        seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
                Log.d("SeekBarDemo", "onStopTrackingTouch " + seekBar.getProgress());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
                Log.d("SeekBarDemo", "onStartTrackingTouch " + seekBar.getProgress());
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                Log.d("SeekBarDemo", "onProgressChanged " + seekBar.getProgress());
                setTitle(String.valueOf(progress));
            }
        });

        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnPlay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                playMusic();
            }
        });

        btnStop = (Button) findViewById(R.id.btnStop);
        btnStop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                stopMusic();
            }
        });

        musicBar = (SeekBar) findViewById(R.id.musicBar);
        musicBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                gotoPlayMusic(musicBar.getProgress());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                pauseMusic();
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
//                gotoPlayMusic(progress);

            }
        });

        timeTextView = (TextView) findViewById(R.id.timeTextView);

    }

    private void playMusic() {
        if (player != null) {
            player.release();
        }

        player = MediaPlayer.create(this, R.raw.here);
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                Toast.makeText(MainActivity.this, "MediaPlayer.onCompletion", Toast.LENGTH_SHORT).show();

            }
        });

        player.start();

        musicBar.setMax(player.getDuration());
        musicBar.setProgress(0);

        startShowMusicProgress();
    }

    private void pauseMusic() {
        stopShowMusicProgress();

        if (player != null) {
            player.pause();
        }
    }


    private void stopMusic() {
        stopShowMusicProgress();

        if (player != null) {
            player.stop();
        }
    }

    private void gotoPlayMusic(int position) {
        if (player != null) {
            player.seekTo(position);
            player.start();

            startShowMusicProgress();
        }
    }

    private void startShowMusicProgress() {
        if (musicTimer != null) {
            musicTimer.cancel();
        }

        musicTimer = new Timer();

        musicTimer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                final int pos = player.getCurrentPosition();
                musicBar.setProgress(pos);


                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        timeTextView.setText(String.valueOf(pos));
                    }
                });

            }

        }, 0, 1000);
    }

    private void stopShowMusicProgress() {
        if (musicTimer != null) {
            musicTimer.cancel();
        }
    }
}